﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace Netcached.Configuration
{
    class NetcachedConfigurationSection : ConfigurationSection
    {
        public NetcachedConfigurationSection()
        {
        }

        [ConfigurationProperty("maxSocketsPerServer", IsRequired = false, DefaultValue = -1)]
        public int MaxSocketsPerServer
        {
            get
            {
                int result;
                bool success =  int.TryParse(base["maxSocketsPerServer"].ToString(), out result);

                return success ? result : -1;
            }
        }

        [ConfigurationProperty("compressionThreshold", IsRequired = false, DefaultValue = 0)]
        public int CompressionThreshold
        {
            get
            {
                int result;
                bool success = int.TryParse(base["compressionThreshold"].ToString(), out result);

                return success ? result : 0;
            }
        }

        [ConfigurationProperty("compressionAlgorithm", IsRequired = false, DefaultValue = CompressionAlgorithm.Deflate)]
        public CompressionAlgorithm CompressionAlgorithm
        {
            get
            {
                CompressionAlgorithm result;
                bool success = Enum.TryParse(base["compressionAlgorithm"].ToString(), true, out result);

                return success ? result : CompressionAlgorithm.Deflate;
            }
        }

        [ConfigurationProperty("servers", IsDefaultCollection = false)]
        [ConfigurationCollection(typeof(ServerCollection), AddItemName = "add", ClearItemsName = "clear", RemoveItemName = "remove")]
        public ServerCollection Servers
        {
            get
            {
                var urlsCollection = (ServerCollection)base["servers"];
                return urlsCollection;
            }
        }
    }

    class ServerCollection : ConfigurationElementCollection
    {
        public ServerCollection()
        {
        }

        protected override ConfigurationElement CreateNewElement()
        {
            return new ServerEntry();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((ServerEntry)element).Address;
        }
    }

    class ServerEntry : ConfigurationElement
    {
        public ServerEntry()
        {
        }

        [ConfigurationProperty("address", IsRequired = true)]
        public string Address
        {
            get { return (string)this["address"]; }
            set { this["address"] = value; }
        }

        [ConfigurationProperty("port", DefaultValue = -1, IsRequired = false)]
        public int Port
        {
            get { return (int)this["port"]; }
            set { this["port"] = value; }
        }

        [ConfigurationProperty("sendReceiveTimeout", DefaultValue = -1, IsRequired = false)]
        public int SendReceiveTimeout
        {
            get { return (int)this["sendReceiveTimeout"]; }
            set { this["sendReceiveTimeout"] = value; }
        }
    }
}
